package com.yunfeisoft.business.dao.impl.postgres;

import com.applet.base.ServiceDaoImpl;
import com.applet.sql.builder.SelectBuilder;
import com.applet.sql.builder.WhereBuilder;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.PmsProductDao;
import com.yunfeisoft.business.model.PmsBrand;
import com.yunfeisoft.business.model.PmsProduct;
import com.yunfeisoft.business.model.PmsProductCategory;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductDaoImpl
 * Description: 商品表Dao实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Repository
public class PmsProductDaoImpl extends ServiceDaoImpl<PmsProduct, String> implements PmsProductDao {

    @Override
    public Page<PmsProduct> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("p.createTime");
        if (params != null) {
            initPageParam(wb, params);
            wb.andFullLike("p.name", params.get("name"));
        }

        SelectBuilder builder = getSelectBuilder("p");
        builder.column("b.name as brandName")
                .column("pc.name as productCategoryName")
                .join(PmsBrand.class).alias("b").on("p.brandId = b.id").build()
                .join(PmsProductCategory.class).alias("pc").on("p.productCategoryId = pc.id").build();

        return queryPage(builder.getSql(), wb);
    }

    @Override
    public List<PmsProduct> queryList(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            wb.andEquals("p.publishStatus", params.get("publishStatus"));
            wb.andFullLike("p.name", params.get("name"));
        }

        SelectBuilder builder = getSelectBuilder("p");
        builder.column("b.name as brandName")
                .column("pc.name as productCategoryName")
                .join(PmsBrand.class).alias("b").on("p.brandId = b.id").build()
                .join(PmsProductCategory.class).alias("pc").on("p.productCategoryId = pc.id").build();

        return query(builder.getSql(), wb);
    }

    @Override
    public List<PmsProduct> listAll(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            wb.orFullLike("name", params.get("queryString"));
            wb.orFullLike("productSn", params.get("queryString"));
        }
        return query(wb);
    }

    @Override
    public Page<PmsProduct> queryPageByBrand(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
            if (!ObjectUtils.isEmpty(params.get("brandId"))) {
                wb.andEquals("brandId", params.get("brandId"));
            }
        }
        return queryPage(wb);
    }

    /**
     * 查询最新商品列表
     * @param params
     * @param limit
     * @return
     */
    @Override
    public List<PmsProduct> queryNewProduct(Map<String, Object> params, int limit) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("createTime");
        if (limit > 0) {
            wb.setPageNum(1);
            wb.setPageSize(limit);
            return queryLimit(wb);
        }
        return query(wb);
    }
}